
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Netty 如何实现TCP的粘包和拆包问题 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>Netty 如何实现TCP的粘包和拆包问题 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">TCP通信中为什么会产生“粘包”和“拆包”问题？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">因为TCP协议本身在应用层并不区分数据包的边界，它将数据视为一个连续的字节流。这导致了多个独立的数据包可能被合并成一个（粘包），或者一个完整的数据包被拆分成多个发送（拆包）。</div>
          </div>
          <div class="card-source">来源: 文档引言及章节「1. 粘包和拆包问题概述」</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">Netty提供了哪种解码器来处理每个消息长度固定的场景？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">Netty提供了 `FixedLengthFrameDecoder` 类。它通过读取预先设定的固定字节数来分割消息，从而解决定长消息的粘包和拆包问题。</div>
          </div>
          <div class="card-source">来源: 2.1 定长消息</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">当消息使用特定分隔符（如“$_”）进行分割时，应该使用Netty的哪个解码器？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">应该使用 `DelimiterBasedFrameDecoder` 类。这个解码器专门用于处理以特定分隔符结尾的消息，非常适合文本协议。</div>
          </div>
          <div class="card-source">来源: 2.2 分隔符分隔</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">`LengthFieldBasedFrameDecoder` 解码器的工作原理是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">它的工作原理是解析消息头部的一个或多个字节作为长度字段。解码器首先读取这个长度值，然后根据该值确定整个消息的边界，从而准确地读取一个完整的消息帧。</div>
          </div>
          <div class="card-source">来源: 2.3 长度字段</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">模式</div>
          <div class="card-question">在处理非常复杂的自定义协议时，如果Netty内置的解码器不适用，应该怎么做？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">模式</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">可以自定义一个解码器。具体做法是继承 `ByteToMessageDecoder` 类，并重写其 `decode` 方法，在方法中实现自定义的协议解析逻辑。</div>
          </div>
          <div class="card-source">来源: 2.4 自定义解码器</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">在自定义 `ByteToMessageDecoder` 时，如何处理接收到的数据不足以构成一个完整消息帧的情况？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">在 `decode` 方法中，首先检查可读字节数是否满足最低要求。如果不满足，直接 `return` 等待更多数据。如果读取了长度字段但发现剩余字节数不够，应调用 `in.resetReaderIndex()` 重置读索引，然后 `return`，以确保下次数据到达时能重新处理该消息。</div>
          </div>
          <div class="card-source">来源: 2.4 自定义解码器</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
